package com.chuanyang.driverh.mainmy.fragment;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.responseresult.ShipperSubscriptionData;
import com.chuanyang.driverh.responseresult.ShipperSubscriptionResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.SwipeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 *
 */

public class SubscribedFragment extends BaseListFragment {
    private HashMap params;
    private SubscriberAdapter adapter;

    @Override
    public BaseListAdapter createAdapter() {
        adapter=new SubscriberAdapter(getActivity());
        return adapter;
    }


    @Override
    protected void initListener() {

    }

    @Override
    public void initView(View view) {
        super.initView(view);
//        adapter.setOnCustomListener(new OnCustomListener()
//        {
//            @Override
//            public void onCustomerListener(View v, int position) {
//                switch (v.getId()) {
//                    case R.id.tv_sub_delete:
//                        deleteDriverSubscription(position);
//                        break;
//                    case R.id.tv_sub_zhiding:
//                        ShipperSubscriptionData.DataBean sunscribe = (ShipperSubscriptionData.DataBean) adapter.getItem(position);
//                        adapter.stick(position,sunscribe);
//                        break;
//
//                }
//            }
//        });
    }

    @Override
    protected void initData() {

    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex() == 9) {
            mCurrentPage = 1;
            getData();
        }


    }

    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"订阅...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());

        MyHttpUtil.selectShipperSubscriptionList(getActivity(),params, new MyCallback<ShipperSubscriptionResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onSuccess(ShipperSubscriptionResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    //登录成功返回用户信息
                    if (result.getData() != null) {
                        setDataResult(result.getData().getData());
                    }

                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });

    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
    }
    //    private void deleteDriverSubscription(final int position)
//    {
//        if (params == null) {
//            params = new HashMap();
//        }
//        params.clear();
//        ShipperSubscriptionData.DataBean bean1= (ShipperSubscriptionData.DataBean) adapter.getItem(position);
//        params.put("id", bean1.getUserId()+"");
//        PersonlApiClient.deleteDriverSubscription(getActivity(), params, new CallBack<Result>() {
//            @Override
//            public void onSuccess(Result result) {
//                if(result.getCode()== Config.OK)
//                {
//                    showMsg("删除订阅成功");
//                    adapter.remove(position);
//
//                }
//            }
//
//        });
//    }
//


    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
        mCurrentPage = 1;
        getData();
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }
    public class SubscriberAdapter extends BaseListAdapter {


        private ArrayList<SwipeLayout> openedItems = new ArrayList<SwipeLayout>();
        private OnCustomListener listener;
        private ViewHolder holder;


        public SubscriberAdapter(Context context) {
            super(context);
        }


        @Override
        public View getZkView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(parent.getContext(), R.layout.my_item_subscribered, null);
                holder = new ViewHolder();
                holder.tv_sub_icon = (ImageView) convertView.findViewById(R.id.tv_sub_icon);
                holder.rb_sub_stars = (RatingBar) convertView.findViewById(R.id.rb_sub_stars);
                holder.tv_sub_name = (TextView) convertView.findViewById(R.id.tv_sub_name);
                holder.tv_act_subbegin = (TextView) convertView.findViewById(R.id.tv_act_subbegin);
                holder.tv_act_subend = (TextView) convertView.findViewById(R.id.tv_act_subend);
                holder.tv_sub_delete = (TextView) convertView.findViewById(R.id.tv_sub_delete);
                holder.tv_sub_zhiding = (TextView) convertView.findViewById(R.id.tv_sub_zhiding);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ShipperSubscriptionData.DataBean bean = (ShipperSubscriptionData.DataBean) adapter.getItem(position);
//            YncApplication.getInstance().loadCircleImage(getActivity(), bean.getUserImage(), R.drawable.shengfeng, R.drawable.shengfeng, holder.tv_sub_icon);
            BitmapUtil.showImgForNetPath(bean.getUserImage(),holder.tv_sub_icon);
            holder.rb_sub_stars.setRating(bean.getStar());
            if (!TextUtils.isEmpty(bean.getUsername())) {
                holder.tv_sub_name.setText(bean.getUsername());
            } else {
                holder.tv_sub_name.setText("");

            }
            if (bean.getShipperLines() != null&&bean.getShipperLines().size()>0)
            {
                if (bean.getShipperLines().get(0).getSPoint() != null) {
                    holder.tv_act_subbegin.setText(bean.getShipperLines().get(0).getSPoint());

                }
                else {
                    holder.tv_act_subbegin.setText("");

                }
                if (bean.getShipperLines().get(0).getEPoint() != null) {
                    holder.tv_act_subend.setText(bean.getShipperLines().get(0).getEPoint());

                } else {
                    holder.tv_act_subend.setText("");

                }
            }
            else {
                holder.tv_act_subbegin.setText("");
                holder.tv_act_subend.setText("");


            }


            return convertView;
        }

        class ViewHolder {
            ImageView tv_sub_icon;
            TextView tv_sub_name;
            TextView tv_act_subbegin;
            TextView tv_sub_delete;
            TextView tv_sub_zhiding;
            TextView tv_act_subend;
            RatingBar rb_sub_stars;
        }


        /**
         * 设置适配器上，某个控件的监听事件
         *
         * @param listener
         */
        public void setOnCustomListener(OnCustomListener listener) {
            this.listener = listener;
        }
    }

}
